Ext.define('ES.view.main.Body',{
	extend:'Ext.tab.Panel',
	alias:'widget.bodycomp',
	requires:['ES.view.home.Home','ES.view.componentDetail.CompDetail','ES.view.archives.Archives'],
	plain:true,
	border:0,
	width:'100%',
    initComponent: function() {
    	this.items=[
	         {
        		title:'Home',
        		bodyPadding:30,
        		icon:'app/assets/home_icon.png',
        		border:0,
      			items:[
      				{
      					xtype:'homecomp'      					
      				}
      			]
        	},
        	{
        		title:'Components',
        		bodyPadding:30,
        		icon:'app/assets/component_icon.gif',
      			border:0,
      			items:[
      				{
      					xtype:'componentspage',
      					id:'componentspage',
      					itemId:'componentspage'
      				}
      			]
        	},
        	{
        		title:'My Archives',
        		bodyPadding:30,
        		icon:'app/assets/archives_icon.png',
        		border:0,
      			items:[
      				{
      					xtype:'archivescomp'
      				}
      			],
      			hidden:false,
      			itemId:'myArchives',
      			id:'myArchives'   			
        	},
        	{
        		title:'People',
        		icon:'app/assets/people_icon.png',
        		bodyPadding:10,
      			html:'People'
        	}
        ];
    	this.callParent(arguments);
    },
    onRender:function(){
    	var me = this;
    	var titleText;
    	var pic;
    	var rating;
    	var overview;
    	var compId;
    	var comments;
    	var envSupport;
    	var langSupport;
    	var version;
    	var openedCompArray = new Array();
    	var openCompTabArray = new Array();
    	Ext.getCmp('componentspage').on('OPEN_COMP_DETAIL_PAGE_EVENT',function(title,pic,rating, overview, compId,comments,envSupport,langSupport,version){
    		if(!me.openedCompArray)
    			me.openedCompArray = new Array();
    		if(!me.openCompTabArray)
    			me.openCompTabArray = new Array();
    		if(Ext.Array.indexOf(me.openedCompArray,compId) != -1)
    		{
    			me.setActiveTab(me.openCompTabArray[Ext.Array.indexOf(me.openedCompArray, compId)]);
    			return;
    		}
    		me.titleText = title;
    		me.pic = pic;
    		me.rating = rating;
    		me.overview = overview;
    		me.compId = compId;
    		me.comments = comments;
    		me.envSupport = envSupport;
    		me.langSupport = langSupport;
    		me.version = version;
    		var tab = me.add({
				    			title:me.titleText,
				        		bodyPadding:60,
				        		icon:'app/assets/component_browser.png',
				        		border:0,
				      			items:[
				      				{
				      					xtype:'compdetail',
				      					pic:me.pic,
				      					title:me.titleText,
				      					rating:me.rating,
				      					overview:me.overview,
				      					compId:me.compId,
				      					comments:me.comments,
				      					envSupport:me.envSupport,
				      					langSupport:me.langSupport,
				      					version:me.version
				      				}
				      			],
				      			closable:true
				    		});
    		me.setActiveTab(tab);
    		me.openedCompArray.push(compId);
    		me.openCompTabArray.push(tab);
    		tab.on('close',function(tab){
    			var index = Ext.Array.indexOf(me.openCompTabArray, tab);
    			Ext.Array.remove(me.openCompTabArray, tab);
    			Ext.Array.remove(me.openedCompArray, me.openedCompArray[index]);
    		})
    	});
    	this.callParent(arguments);
    }
});